package com.meowcook.controller;

import com.meowcook.config.AppConfig;
import com.meowcook.common.AppResult;
import com.meowcook.pojo.dto.LikeCommentDTO;
import com.meowcook.pojo.dto.LikeRecipeDTO;
import com.meowcook.pojo.dto.LikeReplyDTO;
import com.meowcook.pojo.vo.LikeCommentVO;
import com.meowcook.pojo.vo.LikeRecipeVO;
import com.meowcook.pojo.vo.LikeReplyVO;
import com.meowcook.pojo.vo.UserPartIVO;
import com.meowcook.service.ILikeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28792
 * Date: 2025-05-15
 * Time: 17:21
 */
@Slf4j
@Api(tags = "点赞操作接口")
@RestController
@RequestMapping("/like")
public class LikeController {

    @Resource
    private ILikeService likeService;

    @ApiOperation("点赞/取消点赞食谱操作")
    @PostMapping("/thumbsUpRecipe")
    public AppResult thumbsUpRecipe(HttpServletRequest request, @Valid @RequestBody LikeRecipeDTO like) {
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        Long userId = user.getId();
        like.setUserId(userId);
        log.info("食谱点赞数据: {}", like);
        likeService.thumbsUpRecipe(like);
        return AppResult.success("操作成功");
    }

    @ApiOperation("查询用户点赞食谱记录")
    @GetMapping("/getLikeRecipeRecord")
    public AppResult<LikeRecipeVO> getLikeRecipeRecord(HttpServletRequest request,
                                         @ApiParam("食谱id") @RequestParam("recipeId") @NonNull Long recipeId) {
        log.info("食谱id: {}", recipeId);
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        Long userId = user.getId();
        LikeRecipeVO likeRecipeVO = likeService.selectLikeRecipeRecord(userId, recipeId);
        return AppResult.success(likeRecipeVO);
    }

    @ApiOperation("点赞/取消点赞评论操作")
    @PostMapping("/thumbsUpComment")
    public AppResult thumbsUpComment(HttpServletRequest request, @Valid @RequestBody LikeCommentDTO like) {
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        Long userId = user.getId();
        like.setUserId(userId);
        log.info("评论点赞数据: {}", like);
        likeService.thumbsUpComment(like);
        return AppResult.success("操作成功");
    }

    @ApiOperation("查询用户点赞评论记录")
    @GetMapping("/getLikeCommentRecord")
    public AppResult<LikeCommentVO> getLikeCommentRecord(HttpServletRequest request,
                                                  @ApiParam("评论id") @RequestParam("commentId") @NonNull Long commentId) {
        log.info("评论id: {}", commentId);
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        Long userId = user.getId();
        LikeCommentVO likeCommentVO = likeService.selectLikeCommentRecord(userId, commentId);
        return AppResult.success(likeCommentVO);
    }

    @ApiOperation("点赞/取消点赞回复操作")
    @PostMapping("/thumbsUpReply")
    public AppResult thumbsUpReply(HttpServletRequest request, @Valid @RequestBody LikeReplyDTO like) {
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        Long userId = user.getId();
        like.setUserId(userId);
        log.info("回复点赞数据: {}", like);
        likeService.thumbsUpReply(like);
        return AppResult.success("操作成功");
    }

    @ApiOperation("查询用户点赞回复记录")
    @GetMapping("/getLikeReplyRecord")
    public AppResult<LikeReplyVO> getLikeReplyRecord(HttpServletRequest request,
                                                         @ApiParam("评论id") @RequestParam("commentId") @NonNull Long replyId) {
        log.info("评论id: {}", replyId);
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        Long userId = user.getId();
        LikeReplyVO likeReplyVO = likeService.selectLikeReplyRecord(userId, replyId);
        return AppResult.success(likeReplyVO);
    }
}
